/*
 * EnrollPresenter.java
 * business
 *
 * Created by chentao on 2017/4/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.enroll;

import android.text.TextUtils;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.PayWay;
import com.yidiandao.business.data.event.RefreshEnrollResultStateEvent;
import com.yidiandao.business.data.model.IArticleModel;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.data.response.EnrollResponse;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.PayWayListItem;
import com.yidiandao.business.utils.RegexUtils;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Presenter
 */
public class EnrollPresenter implements EnrollContract.Presenter {

    private EnrollContract.View mEnrollView;

    private IArticleModel mArticleModel;

    private Article.Extra mArticleExtra;

    private PayWay mChoosePayWay;

    private float mEnrollMoney;

    private List<DisplayListItem> mPayWayItems;

    private float mTotalMoney;

    private String mRecId;

    public EnrollPresenter(EnrollContract.View view, Article.Extra articleExtra, IArticleModel articleModel) {
        mEnrollView = view;
        mArticleModel = articleModel;
        mArticleExtra = articleExtra;

        mEnrollView.setPresenter(this);
    }

    @Override
    public void start() {
        mEnrollView.setContent(mArticleExtra.getEnrollTitle());

        int maxPerson = mArticleExtra.getMaxPerson();
        mTotalMoney = mEnrollMoney = mArticleExtra.getEnrollMoney();

        if (maxPerson != -1) {
            mEnrollView.setMaxPerson(maxPerson);
        }
        mEnrollView.refreshEnrollMoney(mTotalMoney);

        List<PayWay> payWays = mArticleExtra.getPayWays();
        if (mTotalMoney != 0 && payWays != null && payWays.size() != 0) {
            payWays.get(0).setSelected(true);
            onPayWayChanged(payWays.get(0));

            for (PayWay payWay : payWays) {
                mPayWayItems = new ArrayList<>();
                payWay.setCanChoose(true);
                mPayWayItems.add(new PayWayListItem(payWay));
            }
        } else {
            mEnrollView.setChoosePayWayLayoutVisibility(false);
        }

    }

    @Override
    public void enroll() {
        if (TextUtils.isEmpty(mEnrollView.getPhoneNumber()) || TextUtils.isEmpty(mEnrollView.getUserName())) {
            mEnrollView.showFailedDialog(UIUtils.getString(R.string.information_incompleted));
            return;
        }

        // 检查手机号码格式
        if (!RegexUtils.isPhoneNumber(mEnrollView.getPhoneNumber())) {
            mEnrollView.showFailedDialog(UIUtils.getString(R.string.input_right_number));
            return;
        }

        mArticleModel.enroll(mEnrollView.getPagerTag(), mArticleExtra.getEnrollId(),
                mEnrollView.getUserName(), mEnrollView.getPhoneNumber(), mEnrollView.getPersonNumber(),
                new IArticleModel.OnLoadResultListener<EnrollResponse.Data>() {
                    @Override
                    public void onSuccess(EnrollResponse.Data data, boolean emptyData, boolean hasMore) {

                        mRecId = data.getRecId();
                        String url = data.getUrl();
                        if (!TextUtils.isEmpty(url)) {
                            // 去支付费用
                            mEnrollView.toOnLinePayActivity(url);
                        } else {
                            // 没有Url就不是在线支付,直接显示成功
                            mEnrollView.showSuccessDialog();
                            postRefreshEnrollStateEvent();
                        }
                    }

                    @Override
                    public void onFailed(String message) {
                        mEnrollView.showFailedDialog(message);
                    }
                });
    }

    @Override
    public void onPayWayChanged(PayWay payWay) {
        mChoosePayWay = payWay;
        mEnrollView.refreshPayWay(payWay);
    }

    @Override
    public void onPersonCountChanged(int count) {
        mTotalMoney = mEnrollMoney * count;
        mEnrollView.refreshEnrollMoney(mTotalMoney);
    }

    @Override
    public List<DisplayListItem> getPayWays() {
        return mPayWayItems;
    }

    @Override
    public String getRecId() {
        return mRecId;
    }

    private void postRefreshEnrollStateEvent() {
        EventBus.getDefault().post(new RefreshEnrollResultStateEvent(false, true));
    }
}
